package com.bps.zzant.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sun.reflect.annotation.EnumConstantNotPresentExceptionProxy;

import com.bps.zzant.model.Customer;
import com.bps.zzant.model.CustomerSelection;
import com.bps.zzant.services.CustomerService;


@Controller
public class CustomerJSON {
	
	private static final Logger logger = LoggerFactory.getLogger(CustomerJSON.class);
	
	@Autowired
	private CustomerService customerService;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	@RequestMapping(value = "getCustomerByName.json", method = RequestMethod.GET)
	public Map getCustomerByName(HttpServletRequest request, HttpServletResponse response){
		
		Enumeration<String> a = request.getParameterNames();
	    for (; a.hasMoreElements(); ) {
	        logger.info("????????????"+(String)a.nextElement());
	    }
	        
	        
		logger.info(" +++++++++in CustomerJSON.getCustomerByName. customerName:"+ request.getParameter("customerName"));
		logger.info(" ---------in CustomerJSON.getCustomerByName. term:"+ request.getParameter("term"));
		String customerName = request.getParameter("customerName");
		if(customerName==null)customerName="";
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Customer> customerList = customerService.getCustomersByName(customerName);
		logger.info(" +++++++++in CustomerJSON.getCustomerByName. customerList size:"+customerList.size());
		
		List nameList=new ArrayList();
		
//		for(Iterator i = customerList.iterator(); i.hasNext();){
//			
//			Customer c = (Customer)i.next();
//			CustomerSelection cs = new CustomerSelection();
//			
//			cs.setId(c.getId());
//			cs.setName(c.getCustomerName());
//			
//			nameList.add(cs);			
//		}

		
		// map.put("totalResultsCount", customerList.size());
		
//		map.put("suggestions", nameList);
		map.put("customerList", customerList);
		return map;
		
		
	}
	
}
